﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YunCMS.BLL;
using YunCMS.Model;
using YunCMS.Common;
using YunCMS.Controls.Page;

namespace YunCMS.Web.Admin.Module
{
    public partial class ProductList : ModuleListPage<YCModuleProduct>
    {
        private ModuleBLL<YCModuleProduct> _bll = null;

        protected override ModuleBLL<YCModuleProduct> BLL
        {
            get
            {
                if (_bll == null)
                {
                    _bll = new YCModuleProductBLL();
                }
                return _bll;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {         
            if (!IsPostBack)
            {
                FirstLoad();
            }
        }

        protected override void BindControls(bool isFirst)
        {
            string title = keywordsTB.Text.Trim(),
                check = checkDDL.SelectedValue.Trim(),
                top = topDDL.SelectedValue.Trim();

            if (isFirst)
            {
                AspNetPager1.RecordCount = BLL.GetCount(title, check, top);
            }

            FillRepeater(title, check, top);
        }
        

        private void FillRepeater(string title, string check, string top)
        {
            int pageSize = AspNetPager1.PageSize,
               pageCurrent = AspNetPager1.CurrentPageIndex;

            IList<YCModuleProduct> list = BLL.GetList(title, check, top, pageSize, pageCurrent);

            DataListRP.DataSource = list;
            DataListRP.DataBind();
        }
       
        protected void deleteAllBT_Click(object sender, EventArgs e)
        {
            IList<int> ids = AllSelectedItems(DataListRP);
            foreach (int id in ids)
            {
                BLL.Remove(id);
            }
            BindControls(true);
        }

        protected void activeAllBT_Click(object sender, EventArgs e)
        {
            IList<int> ids = AllSelectedItems(DataListRP);
            foreach (int id in ids)
            {
                BLL.UpdateActive(id, true);
            }
            BindControls(false);
        }

        protected void topAllBT_Click(object sender, EventArgs e)
        {
            IList<int> ids = AllSelectedItems(DataListRP);
            foreach (int id in ids)
            {
                BLL.UpdateIsHome(id, true);
            }
            BindControls(false);
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindControls(false);
        }

        protected void searchBT_Click(object sender, EventArgs e)
        {
            BindControls(true);
        }
    }
}